package com.huangsm.juc.volatile1;

import lombok.Data;

/**
 * 一、volatile关键字
 *
 */
public class TestVolatile {
    public static void main(String[] args) {
        ThreadDemo td=new ThreadDemo();
        new Thread(td).start();
        while (true){
//            synchronized (td){
                if (td.isFlag()){
                    System.out.println("-----------");
                    break;
                }
//            }

        }
    }

}

@Data
class ThreadDemo implements Runnable {
    private volatile   boolean flag = false;
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("flag=" + isFlag());
    }
}
